//
//  BKHomeClassifyCollectionView.m
//  bestkeep
//
//  Created by utouu_mhm on 16/12/14.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKHomeClassifyCollectionView.h"
#import "BKDoubleCollectionViewCell.h"
#import "BKSingleCollectionViewCell.h"
#import "BKClassifyNoDataFooter.h"
#define RGB(r,g,b) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:1]
@implementation BKHomeClassifyCollectionView

-(instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout
{
    if (self = [super initWithFrame:frame collectionViewLayout:layout]) {
        
        self.backgroundColor = RGB(242, 242, 242);
        self.dataSource = self;
        self.delegate = self;
        self.showsVerticalScrollIndicator = NO;
        self.showsHorizontalScrollIndicator = NO;
        [self registerClass:[BKDoubleCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([BKDoubleCollectionViewCell class])];
        [self registerClass:[BKSingleCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([BKSingleCollectionViewCell class])];
        [self registerClass:[BKClassifyNoDataFooter class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:[NSString stringWithFormat:@"%@footer",NSStringFromClass([BKHomeClassifyCollectionView class])]];
    }
    return self;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.showType == singleLineShowOneGoods) {
        
        BKSingleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([BKSingleCollectionViewCell class]) forIndexPath:indexPath];
        cell.singleGoodsModel = self.dataArray[indexPath.row];
        return cell;
    }else{
        
        BKDoubleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([BKDoubleCollectionViewCell class]) forIndexPath:indexPath];
        cell.doubleGoodsModel = self.dataArray[indexPath.row];
        return cell;

    }
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.showType == singleLineShowOneGoods) {
        
        return CGSizeMake(ScreenWidth, 117);
    } else {
        CGFloat height = ScreenWidth * 228/375.0f;
        return CGSizeMake(ScreenWidth/2.0, height);
    }

    
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsZero;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
-(BKClassifyNoDataFooter *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    
    if (kind == UICollectionElementKindSectionFooter && self.isNoData) {
        
        BKClassifyNoDataFooter *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:[NSString stringWithFormat:@"%@footer",NSStringFromClass([BKHomeClassifyCollectionView class])] forIndexPath:indexPath];
        return footer;
    }
    return nil;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    if (self.isNoData) {
        
        return CGSizeMake(ScreenWidth, 85);
    }
    
    return CGSizeZero;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.myDelegate respondsToSelector:@selector(BKCollectionViewWithIndexPath:)]) {
        
        [self.myDelegate BKCollectionViewWithIndexPath:indexPath];
        
    }
}
#pragma mark -滚动的位置的隐藏
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if ([self.myDelegate respondsToSelector:@selector(tableviewCntentOffset:)]) {
        
        [self.myDelegate tableviewCntentOffset:scrollView.contentOffset];
    }
}
#pragma mark =======UIScrollViewDelegate

-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    if (velocity.y == 0) {
        
        return;
    }
    
    
    if ([self.myDelegate respondsToSelector:@selector(BKCollectionViewWithContentOffset:)]) {
        
        [self.myDelegate BKCollectionViewWithContentOffset:velocity];
    }
}


#pragma mark - set方法
-(void)setDataArray:(NSArray *)dataArray
{
    _dataArray = dataArray;
    [self reloadData];
}
-(void)setIsNoData:(BOOL)isNoData
{
    _isNoData = isNoData;
    [self reloadData];
}
-(void)setShowType:(GoodsListTableShowType)showType
{
    _showType = showType;
    [self reloadData];
}

@end

